import axios from 'axios'
import Qs from 'qs'
import store from './store'
import config from './config'

axios.defaults.baseURL = config.url;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
axios.defaults.withCredentials = false;
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // if((typeof config.data)=='object' && config.data.authorization){
  //   config.headers['Authorization'] = 'Bearer '+App.global.loginData.access_token;
  // }else if(config.hasOwnProperty('params') && (typeof config.params)=='object' && config.params.authorization){
  //   config.headers['Authorization'] = 'Bearer '+App.global.loginData.access_token;
  // }
  if ((typeof config.data) == 'object' && config.data.global_loading_hide) {
    // store.state.ework.loading = false;
    config.data.global_loading_hide = null;
  } else if (config.hasOwnProperty('params') && (typeof config.params) == 'object' && config.params.global_loading_hide) {
    // store.state.ework.loading = false;
    config.params.global_loading_hide = null;
  } else {
    // store.state.ework.loading = true;
  }
  const token = window.localStorage.getItem('token')
  if (config.method == 'post') {
    if (token) {
      config.data["token"] = token
    }
    config.data = Qs.stringify(config.data);
  } else {
    if (token) {
      config.params["token"] = token
    }
  }
  return config;
}, function (error) {
  // store.state.ework.loading = false;
  // Message.error('服务器请求错误');
  return Promise.reject(error);
});

function shouquan() {
  var index_url = 'http://zhiyang.yizehd.com/qiangpiao'
  window.localStorage.removeItem('token')
  window.location.href = 'http://zhiyang.yizehd.com/api/auth/auth?state=1&uri=' + index_url;
  // window.open('http://zhiyang.yizehd.com/api/auth/auth?state=1&uri=' + index_url)
  return
}

// 添加响应拦截器 0.
axios.interceptors.response.use(function (response) {
  // store.state.ework.loading = false;
  //错误拦截
  if (response.data.code == 401) {
    shouquan();
  }
  if (response.data.code != 0) {
    if (typeof response.config.params == 'object' && response.config.params.message_hide) {

    } else if (response.config.hasOwnProperty('params') && (typeof response.config.params) == 'object' && response.config.params.message_hide) {

    } else {
      // Message.error(response.data.description);
    }
  }
  return response;
}, function (err) {
  if (err) {
    // store.state.ework.loading = false;
  }
  if (err && err.response) {
    switch (err.response.code) {
      case 400:
        err.message = '请求错误'
        break

      case 401:
        err.message = '未授权，请登录'
        break

      case 403:
        err.message = '拒绝访问'
        break

      case 404:
        err.message = `请求地址出错: ${err.response.config.url}`
        break

      case 408:
        err.message = '请求超时'
        break

      case 500:
        err.message = '服务器内部错误'
        break

      case 501:
        err.message = '服务未实现'
        break

      case 502:
        err.message = '网关错误'
        break

      case 503:
        err.message = '服务不可用'
        break
      case 504:
        err.message = '网关超时'
        break
      case 505:
        err.message = 'HTTP版本不受支持'
        break
      default:
    }
  }
  // Message.error(err.message);
  return Promise.reject(err);
});
export default axios;
